#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class GetNext
{
private:
     list<list<int> > lst;
     list<list<int> >::iterator row;
     list<int>::iterator col;
	 int val;
	 bool hasNxt;
public:
     GetNext(list<list<int> > &dt)
     :lst(dt), hasNxt(false) {
         row = lst.begin();
         col = row->begin();
		 move();
     };

	 void move() {
		 while (row != lst.end() && col == row->end()) {
			 row++;
			 if (row != lst.end()) col = row->begin();
		 }
	 }

	 bool hasNext() {
		return row != lst.end(); 
	 }

	 int getNext()
	 {
		 int rtn = *col++;
		 move();
		 return rtn;
	 }
};


int main(int argc, char **argv)
{

    cout << "-----------------Test 1--------------------" << endl;
    int test[] = {1,2,3,4,5,6};
    int test0[] = {};
    int test1[] = {7,8,9,10};
    int test2[] = {11};
    int test3[] = {12, 13, 14, 15};
    list<list<int> > llst;
    list<int> list0(test, test + sizeof(test)/sizeof(int));
    list<int> list1(test1, test1 + sizeof(test1)/sizeof(int));
    list<int> list2(test2, test2 + sizeof(test2)/sizeof(int));
    list<int> list3(test3, test3 + sizeof(test3)/sizeof(int));
    list<int> listNull;
	llst.push_back(listNull);
	llst.push_back(listNull);
	llst.push_back(listNull);
	llst.push_back(listNull);
	llst.push_back(listNull);
	llst.push_back(listNull);
    llst.push_back(list0);
    llst.push_back(list1);
    llst.push_back(listNull);
	llst.push_back(listNull);
    llst.push_back(list2);
    llst.push_back(list3);
	llst.push_back(listNull);
	llst.push_back(listNull);
	llst.push_back(listNull);

    GetNext iter(llst);
	while (iter.hasNext()) {
        int i = iter.getNext();
        cout << i << " ";
    }
    cout << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;
}

